package com.lijie.bs_recruit.Util;

import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @Classname uploadUtil
 * @Description TODO
 * @Date 2021/4/19 9:56
 * @Created by lijie
 */
@Component
public class uploadUtil {
    public String headerImg(MultipartFile file, String path) {
        //原文件名
        String originFileName = file.getOriginalFilename();
        //新的文件名
        String newFileName = UUID.randomUUID() + originFileName.substring(originFileName.lastIndexOf("."));
        //目标文件地址
        String desName = path + newFileName;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(desName);
            //复制
            FileCopyUtils.copy(file.getInputStream(),fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newFileName;
    }
    public String filesUpload( MultipartFile file,String path) throws IOException {
        //原文件名
        String originFileName = file.getOriginalFilename();
        //新的文件名
        String newFileName = UUID.randomUUID() + originFileName.substring(originFileName.lastIndexOf("."));

        //如果文件夹不存在，创建
        File fileP = new File(path);
        if (!fileP.isDirectory()) {
            //递归生成文件夹
            fileP.mkdirs();
        }
        file.transferTo(new File(fileP,newFileName));
        return newFileName;
    }

}
